#include "glad/glad.h"

#include "VisWidget.h"
#include <vector>
#include "vmath.h"

#include "sceneim.h"

#include <QTimer>

std::vector<vmath::vec3> vts;
std::vector<vmath::vec3> cols;

VisWidget::VisWidget(QWidget *parent)
	: QOpenGLWidget(parent)
{
	ui.setupUi(this);
	
	vts.push_back(vmath::vec3(0, 0, 0));
	vts.push_back(vmath::vec3(100, 0, 0));
	vts.push_back(vmath::vec3(50, 50, 0));
	vts.push_back(vmath::vec3(0, 0, 0));

	cols.resize(4, vmath::vec3(1, 1, 1));

	float* dt_ = (float*)&vts[0];

	QTimer* _timer = new QTimer(this);
	connect(_timer, SIGNAL(timeout()), this, SLOT(update()));
	_timer->start(10);

}

VisWidget::~VisWidget()
{
}

void VisWidget::initializeGL()
{
	// initializeOpenGLFunctions();
	gladLoadGL();

	glClearColor(0.2, 0.2, 0.4, 1);
	glClearDepth(0.0);

	glDisable(GL_DEPTH_TEST);
	// glEnable(GL_CULL_FACE);

	initContext();

}

void VisWidget::mousePressEvent(QMouseEvent* event)
{
	int posx = event->pos().x();
	int posy = event->pos().y();

	int btn = GLUT_LEFT_BUTTON;
	if (event->button() == Qt::MouseButton::LeftButton)
		btn = GLUT_LEFT_BUTTON;
	else if (event->button() == Qt::MouseButton::MiddleButton)
		btn = GLUT_MIDDLE_BUTTON;
	else if (event->button() == Qt::MouseButton::RightButton)
		btn = GLUT_RIGHT_BUTTON;

	int btnstate = GLUT_DOWN;

	mouseCB(btn, btnstate, posx, posy);
}
void VisWidget::mouseReleaseEvent(QMouseEvent* event)
{
	int posx = event->pos().x();
	int posy = event->pos().y();

	int btn = GLUT_LEFT_BUTTON;
	if (event->button() == Qt::MouseButton::LeftButton)
		btn = GLUT_LEFT_BUTTON;
	else if (event->button() == Qt::MouseButton::MiddleButton)
		btn = GLUT_MIDDLE_BUTTON;
	else if (event->button() == Qt::MouseButton::RightButton)
		btn = GLUT_RIGHT_BUTTON;

	int btnstate = GLUT_UP;

	mouseCB(btn, btnstate, posx, posy);
}

void VisWidget::mouseMoveEvent(QMouseEvent* event)
{
	int posx = event->pos().x();
	int posy = event->pos().y();
	mouseMotionCB(posx, posy);
}

void VisWidget::resizeGL(int w, int h)
{

	glViewport(0, 0, (GLsizei)w, (GLsizei)h);

	reshapeCB(w, h);
	return;

	// Calculate aspect ratio
	qreal aspect = qreal(w) / qreal(h ? h : 1);

	// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
	const qreal zNear = 10.0, zFar = 70.0, fov = 45.0;

	// Reset projection
	// projection.setToIdentity();
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// Set perspective projection
	GLdouble half_h = h * 0.5;
	GLdouble half_w = aspect * half_h;
	glOrtho(-half_w, half_w, -half_h, half_h, zNear, zFar);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
//! [5]

void VisWidget::paintGL()
{
	displayCB();
	return;

	// Clear color and depth buffer
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glVertexPointer(3, GL_FLOAT, 0, (void*)((float*)(&vts[0])));
	glColorPointer(3, GL_FLOAT, 0, (void*)((float*)(&cols[0])));

	glPushMatrix();
	glLoadIdentity();
	glTranslated(0, 0, -20.0);


	glDrawArrays(GL_LINE_STRIP, 0, 4);

	glPopMatrix();

	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);

	//glPushMatrix();
	//glTranslatef(0, 0, -20);

	//glBegin(GL_TRIANGLES);
	//glColor3f(1, 1, 1);
	//glVertex3f(0, 0, 1);
	//glColor3f(1, 1, 0);
	//glVertex3f(100, 0, 1);
	//glColor3f(1, 1, 1);
	//glVertex3f(50, 50, 1);
	//glEnd();

	//glPopMatrix();

	// update();
}

